
<template>
  <div class="app-container action-active-container">
    <div class="back-content flex-between">
      <div class="back-main flex-normal">
        <span class="back-icon el-icon-back" @click="$router.go(-1)"></span>
        <span class="back-label">活动效果</span>
      </div>
    </div>
    <div class="action-active-main">
      <div class="data-left-content">
        <div class="mode-item active-info-content">
          <div class="row-item flex-normal">
            <span class="item-label">活动类型</span>
            <span class="item-val">{{activeInfo.activityTypeId | numToStr(dictsList, 'activityType')}}</span>
          </div>
          <div class="row-item flex-normal">
            <span class="item-label">活动名称</span>
            <span class="item-val">{{activeInfo.activityName}}</span>
          </div>
        </div>
        <div class="mode-item active-data-content">
          <div class="model-title-content flex-between">
            <span class="title-txt">活动数据</span>
          </div>
          <div class="data-list-content flex-between">
            <div class="item flex-column">
              <span class="item-val">{{browseInfo.visitorsNum}}</span>
              <span class="item-label">浏览人数</span>
            </div>
            <div class="item flex-column">
              <span class="item-val">{{browseInfo.dealNum}}</span>
              <span class="item-label">成交人数</span>
            </div>
            <div class="item flex-column">
              <span class="item-val">{{browseInfo.salesVolume}}</span>
              <span class="item-label">总销售额（元）</span>
            </div>
            <div class="item flex-column">
              <span class="item-val">{{browseInfo.turnoverRate}} %</span>
              <span class="item-label">流失率</span>
            </div>
          </div>
        </div>
        <div class="mode-item active-echarts-content" v-loading="totalSalesProp.loading">
          <div class="model-title-content flex-between">
            <span class="title-txt">总销售额（元）</span>
          </div>
          <div class="total-price">{{ totalSalesProp.totalSales }}</div>
          <div ref="echarts-data" class="echarts-data"></div>
        </div>
      </div>
      <div class="data-right-content">
        <div class="mode-item">
          <div class="model-title-content flex-between">
            <span class="title-txt">活动数据</span>
            <el-button type="text" size="small" @click="exportData">导出活动报表</el-button>
          </div>
          <div class="app-table-container" v-loading="listInfo.loading">
            <el-table :data="listInfo.list" border width="100%">
              <el-table-column
                label="姓名"
                align="center"
                prop="name"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="电话"
                align="center"
                prop="phone"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="用户定位"
                align="center"
                prop="address"
                :show-overflow-tooltip="true">
                <template slot-scope="{row}">
                  {{row.address == 'null' ? '' : row.address}}
                </template>
              </el-table-column>
              <el-table-column
                label="用户来源"
                align="center"
                prop="userSource"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="最近浏览时间"
                align="center"
                prop="createTime"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="浏览次数"
                align="center"
                prop="browseNum"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="成交份数"
                align="center"
                prop="dealNum"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="成交金额(元)"
                align="center"
                prop="salesVolume"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="分享"
                align="center"
                prop="whetherToShare">
                <template slot-scope="{row}">
                    {{row.whetherToShare == 1 ? '是' : '否'}}
                  </template>
                </el-table-column>
              <el-table-column          
                label="分享佣金(元)"
                align="center"
                prop="completePrice"
                :show-overflow-tooltip="true"
              />
            </el-table>
            <pagination
              v-show="listInfo.total > 0"
              :total="listInfo.total"
              :page.sync="listInfo.queryParams.pageNum"
              :limit.sync="listInfo.queryParams.pageSize"
              @pagination="getList"
            />    
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import {getActivityDetail} from '@/api/activeManage/active.js'
import {getActivityBrowseInfo, exportData, echartsData, getUserList} from '@/api/activeManage/active-data.js'
let Ebase = +new Date(1988, 9, 3);
let EoneDay = 24 * 3600 * 1000;
let Edata = [[Ebase, Math.random() * 300]];
for (let i = 1; i < 20000; i++) {
  let now = new Date((Ebase += EoneDay));
  Edata.push([+now, Math.round((Math.random() - 0.5) * 20 + Edata[i - 1][1])]);
}
export default {
  name: "storeActiveData",
  data() {
    return {
      dictsList: [
        { label: "活动类型", name: "activityType", deptType: "activityType", child: [] },
      ],
      activeInfo: {},
      browseInfo: {},
      totalSalesProp: {
        totalSales: '0.00',
        loading: false
      },
      listInfo: {
        loading: false,
        list: [],
        total: 0,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          activityId: this.$route.params['id'] || ''
        }
      },
      options: {
        tooltip: {
          trigger: 'axis',
          position: function (pt) {
            return [pt[0], '10%'];
          }
        },
        xAxis: {
          type: 'time',
          boundaryGap: false
        },
        yAxis: {
          type: 'value',
          boundaryGap: [0, '100%']
        },
        dataZoom: [
          {
            type: 'inside',
            start: 0,
            end: 20
          },
          {
            start: 0,
            end: 20
          }
        ],
        series: [
        {
          type: 'line',
          name: '销售量',
          smooth: true,
          symbol: 'circle',
          symbolSize: 5,
          sampling: 'average',
          itemStyle: {
            color: '#0770FF'
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(58,77,233,0.8)'
              },
              {
                offset: 1,
                color: 'rgba(58,77,233,0.3)'
              }
            ])
          },
          data: []
        },
        {
          type: 'line',
          name: '浏览量',
          smooth: true,
          symbol: 'circle',
          symbolSize: 5,
          sampling: 'average',
          itemStyle: {
            color: '#F2597F'
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(213,72,120,0.8)'
              },
              {
                offset: 1,
                color: 'rgba(213,72,120,0.3)'
              }
            ])
          },
          data: []
          }
        ]
      }
    };
  },
  filters: {
    numToStr(num, list, type){
      let index = list.findIndex(item => item.name == type)
      if(index == -1) return '未定义参数'
      let idx = list[index].child.findIndex(i => i.dictValue == num)
      if(idx == -1) return '---'
      return list[index].child[idx].dictLabel
    }
  },
  mounted(){
    let _self = this
    _self.initGetDicts().then(async res => {
      _self.queryActiveInfo()
    })
  },
  methods: {
    async queryActiveInfo(){
      let _self = this
      let {id = ''} = _self.$route.params
      await getActivityDetail(id).then(res => {
        _self.activeInfo = res.data
        _self.queryBrowseInfo()
        _self.queryEchartsData()
        _self.getList()
      })
    },
    async queryEchartsData(){
      let _self = this
      let {id = ''} = _self.$route.params
      _self.totalSalesProp.loading = true
      await echartsData(id).then(res => {
        let {verticalView = [], totalSales} = res.data || {}
        _self.totalSalesProp.totalSales = totalSales || '0.00'
        let salesList = verticalView.map(i =>{
          return [new Date(i.date).getTime(), i.salesVolume]
        })
        let visitList = verticalView.map(i =>{
          return [new Date(i.date).getTime(), i.visitorsNum]
        })
        _self.options['series'][0]['data'] = salesList
        _self.options['series'][1]['data'] = visitList
        let barChart = echarts.init(_self.$refs['echarts-data']);
        barChart.setOption(_self.options, true)
      }).finally(() => {
        _self.totalSalesProp.loading = false
      })
    },
    async queryBrowseInfo(){
      let _self = this
      let {id = ''} = _self.$route.params
      await getActivityBrowseInfo(id).then(res => {
        _self.browseInfo = res.data
      })
    },
    async getList(){
      let _self = this
      _self.listInfo.loading = true
      await getUserList(_self.listInfo.queryParams).then(res => {
        _self.listInfo.list = res.rows
        _self.listInfo.total = res.total
      })
      _self.listInfo.loading = false
    },
    exportData(){
      let _self = this, {activityName} = this.activeInfo
      let {id = ''} = _self.$route.params
      exportData({acddActivityId: id}, `${activityName}-活动数据`).then(res => {
        console.log(res)
      })
    },
  },
};
</script>
<style scoped lang="scss">
  @media screen and (min-width: 1688px) {
    .action-active-main {
      grid-template-columns: 565px 1fr;
    }
  }
  @media screen and (max-width: 1689px) {    
    .action-active-main {
      grid-template-columns: repeat(1, 1fr) !important;
    }
  }
.back-content{
  padding: vh(25) vw(20) vh(25) 0;
  .back-icon{
    font-size: vh(36);
    cursor: pointer;
  }
  .back-label{
    margin-left: vw(20);
    font-size: 13px;
    color: #3D3D3D;
  }
}
.action-active-container{
  display: flex;
  flex-direction: column;
  padding-left: vw(20);
  .action-active-main{
    display: grid;
    gap: 20px;
    .data-right-content{
      .mode-item{
        min-height: 70vh;
      }
      .app-table-container{
        padding: 0;
      }
    }
    .mode-item{
      box-shadow: 0px 2px 10px 0px rgba(0, 0, 0, 0.1);
      margin-bottom: 20px;
      padding: 0 20px;
      .model-title-content{
        padding: 12px 0;
        .title-txt{
          font-size: 20px;
          color: #000000;
        }
      }
    }
  }
}
.active-info-content{
  padding: 26px 20px!important;
  .row-item{
    margin-bottom: 15px;
    &:last-child{
      margin-bottom: 0;
    }
    .item-label{
      font-size: 16px;
      color: #767676;
      width: 110px;
    }
    .item-val{
      font-size: 16px;
      color: #000000;
    }
  }
}
.data-list-content{
  padding-bottom: 23px !important;
  .item{
    width: 120px;
    box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.302);
    border: 1px solid #DDDDDD;
    padding: 19px 0;
    text-align: center;
    .item-val{
      font-size: 24px;
      font-weight: 500;
      color: #3D3D3D;
    }
    .item-label{
      margin-top: 48px;
      font-size: 12px;
      color: #3D3D3D;
    }
  }
}
.active-echarts-content{
  padding: 20px !important;
  .total-price{
    color: #000000;
    font-size: 32px;
    font-weight: 500;
    padding: 4px 0;
  }
  .echarts-data{
    height: 288px;
  }
}
</style>